package com.chandon.T05_timeApi.T01_temporal;

import java.time.*;
import java.time.temporal.ChronoUnit;

import static java.time.temporal.ChronoField.EPOCH_DAY;

/**
 * 描述
 *
 * @author Dong
 * @version 1.0
 * @date 2021-04-20 20:52
 */

public class T02_LocalDateTest {

    public static void main(String[] args) {

        // 当前日期 2021-04-20
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);


        // 指定日期
        LocalDate date = LocalDate.of(2021, 4, 20);
        System.out.println(date);

        // 1970-01-01 为基准加减天数
        LocalDate ofEpochDay = LocalDate.ofEpochDay(localDate.getLong(EPOCH_DAY));
        System.out.println(ofEpochDay);

        System.out.println("----------------------");


        // 字符串解析为日期
        LocalDate date1 = LocalDate.parse("2021-04-20");
        System.out.println(date1);

        // 获取2021-04-20 的年月日
        System.out.println(date1.getYear());
        // 英语 月份
        System.out.println(date1.getMonth());
        // 所在年份的第几天
        System.out.println(date1.getDayOfYear());
        // 所在月份的第几天
        System.out.println(date1.getDayOfMonth());
        // 周几 英语
        System.out.println(date1.getDayOfWeek());
        // 周第几天
        System.out.println(date1.getDayOfWeek().getValue());

        // 闰年判断
        System.out.println(date1.isLeapYear());

        System.out.println("----------------------");

        // 条件查询
        Long days = LocalDate.now().query(temporal -> {

            LocalDate localDate1 = LocalDate.from(temporal);
            LocalDate localDate2 = LocalDate.of(localDate1.getYear() + 1, Month.MAY, 1);
            MonthDay.of(Month.MAY, 1);

            return ChronoUnit.DAYS.between(localDate1, YearMonth.of(Year.now().plusYears(1).getValue(), Month.MAY).atDay(1));
        });
        System.out.println(days);


        // 返回当前日期以后某个时间日期（天，周，月，年，几十年，世纪，千年，）
        LocalDate date2 = LocalDate.now().plus(1, ChronoUnit.DAYS);
        LocalDate date3 = LocalDate.now().plus(1, ChronoUnit.WEEKS);
        LocalDate date4 = LocalDate.now().plus(1, ChronoUnit.MONTHS);
        LocalDate date5 = LocalDate.now().plus(1, ChronoUnit.YEARS);
        LocalDate date6 = LocalDate.now().plus(1, ChronoUnit.DECADES);
        LocalDate date7 = LocalDate.now().plus(-1, ChronoUnit.CENTURIES);
        LocalDate date8 = LocalDate.now().plus(-1, ChronoUnit.MILLENNIA);


        /*
            2021-04-21
            2021-05-20
            2022-04-20
            2031-04-20
            2121-04-20
            3021-04-20
         */
        System.out.println(date2);
        System.out.println(date3);
        System.out.println(date4);
        System.out.println(date5);
        System.out.println(date6);
        System.out.println(date7);
        System.out.println(date8);
        System.out.println(LocalDate.MAX);
    }

}
